package com.zjw.zy.ext

import android.content.Context
import androidx.lifecycle.viewModelScope
import com.zjw.zy.AppService
import com.zjw.zy.aidl.binder.IAppServiceBinder
import com.zjw.zy.base.BaseViewModel
import com.zjw.zy.utils.AppUtils
import com.zjw.zy.utils.taskpool.TaskPool
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/9/26 17:32
 * @description：
 */


fun BaseViewModel.onConcurrent(task: () -> Unit) {
    TaskPool.concurrent().post { task() }
}

fun BaseViewModel.onIo(task: () -> Unit) {
    TaskPool.sequential().post(task)
}


fun BaseViewModel.onMain(task: () -> Unit) {
    TaskPool.main().post { task() }
}

fun BaseViewModel.requireService() = AppUtils.getMainActivity().service()


fun BaseViewModel.launchInDefault(
    context: CoroutineContext = Dispatchers.Default,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> Unit
): Job {
    return this.viewModelScope.launch(context, start, block)
}

fun BaseViewModel.launchInMain(
    context: CoroutineContext = Dispatchers.Main,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> Unit
): Job {
    return this.viewModelScope.launch(context, start, block)
}

fun BaseViewModel.launchInIo(
    context: CoroutineContext = Dispatchers.IO,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> Unit
): Job {
    return this.viewModelScope.launch(context, start, block)
}


fun BaseViewModel.activity(): Context {
    return AppUtils.getMainActivity()
}